﻿using System;

namespace LpbPrj.Client
{
    /// <summary>
    /// 数学计算公式
    /// 特别注意：四舍五入的地方
    /// 必须使用第七个重载方法：Math.Round(Decimal, Int32, MidpointRounding)  （举例：Math.Round((decimal)526.925, 2,MidpointRounding.AwayFromZero)）
    /// 不能直接使用double进行计算
    /// </summary>
    public static class NumbersExtend
    {
        public static double String2Double(this string str)
        {
            double ret = 0.0;
            if (string.IsNullOrEmpty(str)) return ret;

            if (double.TryParse(str, out double data))
            {
                ret = data;
            }
            return ret;
        }

        public static string ToStringEx(this object value)
        {
            if (value != null)
            {
                return value.ToString().Replace(" ", "");
            }
            return null;
        }

        public static bool IsNaN(this double value) => double.IsNaN(value);

        public static int? ToInt(this int value)
        {
            if (value <= 0) return null;
            return value;
        }

        public static double? ToDouble(this double value, int? digits = null)
        {
            if (double.IsNaN(value)) return null;

            double roundDouble = value;
            if (digits.HasValue)
            {
                roundDouble = (double)Math.Round((decimal)value, digits.Value, MidpointRounding.AwayFromZero);
            }
            return roundDouble;
        }

        public static double? ToDouble(this double? value, int? digits = null)
        {
            if (!value.HasValue) return null;

            double roundDouble = value.Value;
            if (digits.HasValue)
            {
                roundDouble = (double)Math.Round((decimal)value, digits.Value, MidpointRounding.AwayFromZero);
            }
            return roundDouble;
        }

        public static decimal? ToDecimal(this double value, int? digits = null)
        {
            if (double.IsNaN(value)) return null;

            double roundDouble = value;
            if (digits.HasValue)
            {
                roundDouble = (double)Math.Round((decimal)value, digits.Value, MidpointRounding.AwayFromZero);
            }
            return Convert.ToDecimal(roundDouble);
        }

        public static int? ToInt(this double value)
        {
            if (double.IsNaN(value)) return null;
            else return (int)Math.Round((decimal)value, 0, MidpointRounding.AwayFromZero);
        }

        public static byte? ToByte(this double value)
        {
            if (double.IsNaN(value)) return null;
            else return (byte)Math.Round((decimal)value, 0, MidpointRounding.AwayFromZero);
        }

        public static double? ToKR(this double? param)
        {
            if (!param.HasValue || ((param.Value - 0.0001) < 0)) return null;
            //double ret = 33.75 * 10 / (double)param;
            //return System.Convert.ToDecimal(Math.Round(ret, 2, MidpointRounding.AwayFromZero));
            return 33.75 * 10 / param.Value;
        }

        public static string ToShowString(this double? value, int digits = 2, string substitutes = null, string prefix = null, string suffix = null)
        {
            string output = null;
            try
            {
                if (value.HasValue && !double.IsNaN(value.Value))
                {
                    double roundDouble = (double)Math.Round((decimal)value, digits, MidpointRounding.AwayFromZero);

                    output = digits == 0 ? ((int)roundDouble).ToString() : roundDouble.ToString(string.Format("f{0}", digits));

                    if (!string.IsNullOrEmpty(prefix))
                    {
                        output = prefix + output;
                    }

                    if (!string.IsNullOrEmpty(suffix))
                    {
                        output = output + suffix;
                    }
                }
                else
                {
                    output = substitutes;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                output = null;
            }

            return output;
        }

        public static string ToFormatPowerString(this double? value, int digits = 2, string substitutes = null, string prefix = null, string suffix = null)
        {
            string output = null;
            if (value.HasValue && !double.IsNaN(value.Value))
            {
                double roundDouble = (double)Math.Round((decimal)value, digits, MidpointRounding.AwayFromZero);
                if (roundDouble < 0.0)
                {
                    //output = string.Format("{0:0.00}", roundDouble);
                    output = roundDouble.ToString(string.Format("f{0}", digits));
                }
                else
                {
                    //output = string.Format("{0:+0.00}", roundDouble);
                    output = roundDouble.ToString(string.Format("f{0}", digits));
                    output = "+" + output;
                }


                if (!string.IsNullOrEmpty(prefix))
                {
                    output = prefix + output;
                }

                if (!string.IsNullOrEmpty(suffix))
                {
                    output = output + suffix;
                }
            }
            else
            {
                output = substitutes;
            }

            return output;
        }

        public static string ToPercentString(this double? value, double dataTotal = 1.0, int digits = 0, string suffix = "%")
        {
            string output = null;
            if (value.HasValue && !double.IsNaN(value.Value))
            {
                double dataConvert = (double)Math.Round((decimal)(value / dataTotal * 100), digits, MidpointRounding.AwayFromZero);
                output = (digits == 0 ? ((int)dataConvert).ToString() : dataConvert.ToString(string.Format("f{0}", digits)));

                if (!string.IsNullOrEmpty(suffix))
                {
                    output = output + suffix;
                }
            }

            return output;
        }

        public static string ToShowString(this decimal? value, int digits = 2, string substitutes = "-", string prefix = null, string suffix = null)
        {
            return ((double?)value).ToShowString(digits, substitutes, prefix, suffix);
        }

        public static string ToPercentString(this decimal? value, double dataTotal = 1.0, int digits = 0, string suffix = "%")
        {
            return ((double?)value).ToPercentString(dataTotal, digits, suffix);
        }

        public static string ToShowString(this int? value, string prefix = null, string suffix = null)
        {
            string output = null;
            if (value.HasValue && !double.IsNaN(value.Value))
            {
                int roundDouble = value.Value;
                if (!string.IsNullOrEmpty(prefix))
                {
                    //if (prefix.Equals("+"))
                    //{
                    //    if (roundDouble >= 0)
                    //    {
                    //        output = "-" + roundDouble.ToString();
                    //    }
                    //    else
                    //    {
                    //        output = "+" + roundDouble.ToString();
                    //    }
                    //}
                    //if (prefix.Equals("-"))
                    //{
                    //    if (roundDouble >= 0)
                    //    {
                    //        output = "+" + roundDouble.ToString();
                    //    }
                    //    else
                    //    {
                    //        output = "-" + roundDouble.ToString();
                    //    }
                    //}
                    output = prefix + roundDouble.ToString();
                }
                else
                {
                    output = roundDouble.ToString();
                }

                if (!string.IsNullOrEmpty(suffix))
                {
                    output = output + suffix;
                }
            }
            return output;
        }


    }


}